/**
 * @description html转换按钮工具
 *  2013-5-21
 */
Ccay.Module.define("Ccay.example.ui.buttons2menuBtn",function(page,$S){
	// 初始化图标下拉选择
	var imgTypes = [
             { value: "add", text: 'add添加' },
             { value: "del", text: 'del删除' },
             { value: "pre-del", text: 'pre-del预删除' },
             { value: "edit", text: 'edit编辑' },
             { value: "save", text: 'save保存' },
             { value: "import", text: 'import导入' },
             { value: "export", text: 'export导出' },
             { value: "search", text: 'search搜索' },
             { value: "confirm", text: 'confirm确认' },
             { value: "cancel", text: 'cancel取消' },
             { value: "default", text: 'default默认' },
             { value: "view", text: 'view查看' },
             { value: "replace", text: 'replace替换' },
             { value: "confirm", text: 'confirm确认' },
             { value: "close", text: 'close关闭' },
             { value: "back", text: 'back后退' },
             { value: "download", text: 'download下载' },
             { value: "upload", text: 'upload上传' },
             { value: "end", text: 'end完成' },
             { value: "reset", text: 'reset重置' },
             { value: "copy", text: 'copy复制' },
             { value: "setting", text: 'setting设置' },
             { value: "refresh", text: 'refresh刷新' },
             { value: "undo", text: 'undo撤销' },
             { value: "pager-first", text: 'pager-first首页' },
             { value: "pager-prev", text: 'pager-prev前一页' },
             { value: "pager-next", text: 'pager-next后一页' },
             { value: "pager-prev", text: 'pager-prev前一页' },
             { value: "pager-last", text: 'pager-last最后一页' },
             { value: "pager-goto", text: 'pager-goto跳转' },
             { value: "sort", text: 'sort排序' },
             { value: "asce", text: 'asce升序' },
             { value: "desc", text: 'desc降序' },
             { value: "menu-downarrow", text: 'menu-downarrow展开' },
             { value: "pause", text: 'pause暂定' },
             { value: "restore", text: 'restore恢复' },
             { value: "lock", text: 'lock锁上' },
             { value: "dialog-close", text: 'dialog-close关闭' },
             { value: "tab-close", text: 'tab-close关闭' },
             { value: "print", text: 'print打印' },
             { value: "preview", text: 'preview预览' },
             { value: "execute", text: 'execute执行' },
             { value: "commission", text: 'commission授权' },
             { value: "preview", text: 'preview预览' },
             { value: "enable", text: 'enable开启' },
             { value: "disable", text: 'disable禁用' },
             { value: "yes", text: 'yes是' },
             { value: "no", text: 'no否' },
             { value: "invalid", text: 'invalid无效' },
             { value: "valid", text: 'valid有效' },
             { value: "on-line", text: 'on-line在线' },
             { value: "leave", text: 'leave离开' },
             { value: "busy", text: 'busy忙' },
             { value: "off-line", text: 'off-line离线' },
			 { value: "status-new", text: 'status-new新' },
			 { value: "status-draft", text: 'status-draft草稿' },
			 { value: "no-del", text: 'no-del禁止删除' },
			 { value: "no-premission", text: 'no-premission无权限' },
			 { value: "menu-expend", text: 'menu-expend菜单展开' },
			 { value: "menu-shrink", text: 'menu-shrink菜单收缩' },
			 { value: "show", text: 'show显示' },
			 { value: "hidden", text: 'hidden隐藏' },
			 { value: "folder-closed", text: 'folder-closed文件夹收缩' },
			 { value: "folder-opened", text: 'folder-opened文件夹展开' },
			 { value: "open", text: 'open打开' },
			 { value: "close", text: 'close关闭' },
			 { value: "checked", text: 'checked勾选' },
			 { value: "unchecked", text: 'unchecked不勾选' },
			 { value: "pre-checked", text: 'pre-checked预勾选' },
			 { value: "radio-checked", text: 'radio-checked' },
			 { value: "radio-unchecked", text: 'radio-unchecked' },
			 { value: "link", text: 'link链接' },
			 { value: "admin", text: 'admin管理员' },
			 { value: "group", text: 'group群组' },
			 { value: "log", text: 'log日志' },
			 { value: "help", text: 'help帮助' },
			 { value: "warning", text: 'warning警告' },
			 { value: "female", text: 'female女士' },
			 { value: "male", text: 'male男士' },
			 { value: "attachment", text: 'attachment附件' },
			 { value: "mail", text: 'mail邮箱' },
			 { value: "msg", text: 'msg消息' },
			 { value: "date", text: 'date日历' },
			 { value: "statistics", text: 'statistics统计' },
			 { value: "pic", text: 'pic图片' },
			 { value: "flow", text: 'flow流' },
			 { value: "callback", text: 'callback回调' },
			 { value: "excel", text: 'excel' },
			 { value: "panel-normal", text: 'panel-normal' },
			 { value: "panel-minimize", text: 'panel-minimize' },
			 { value: "panel-maximize", text: 'panel-maximize' }
    ];
	page.ready=function(){
		$S("#2menu").ccayTab();
		
	
		$S("#2menubutton").ccayMenuButton({
			    defaultText : "按钮集合",
				menus : [ 
				  {
					icon : "add", // 指定系统图片的名称，可在“按钮”demo中查询，也可指定之际的类名，自定义图片样式
					id : "btnAdd", // 指定按钮id
					permission:"Service$HtmlArea$create", //ccayPermission
					text : "ccay.common.button.add", // i18nKey
					click : "alert(1)" // 按钮事件
				 }, 
				 {
					icon : "del", // 指定系统图片的名称，可在“按钮”demo中查询，也可指定之际的类名，自定义图片样式
					id : "btnDel", // 指定按钮id
					permission:"Service$HtmlArea$delete", //ccayPermission
					text : "ccay.common.button.delete", // i18nKey
					click : "alert(2)" // 按钮事件
				 }, 
				 {
					icon : "import", // 指定系统图片的名称，可在“按钮”demo中查询，也可指定之际的类名，自定义图片样式
					id : "btnImport", // 指定按钮id
					permission:"Service$HtmlArea$import", //ccayPermission
					text : "ccay.common.button.import", // i18nKey
					click : "alert(3)" // 按钮事件
				 },{
					icon : "export", // 指定系统图片的名称，可在“按钮”demo中查询，也可指定之际的类名，自定义图片样式
					id : "btnExport", // 指定按钮id
					permission:"Service$HtmlArea$export", //ccayPermission
					text : "ccay.common.button.export", // i18nKey
					click : "alert(4)" // 按钮事件
				 } 
				 ]
		});
	};
	
	page.setImgSelector = function(){
		$S("#btnResourceDiv1").empty();
		$S("#myTable").empty();
		$S("#btnResourceDiv1").append($S("#btnResource1").val());
		var getBtns = $S("#btnResourceDiv1").find("input[type='button']"),tempTable = [];
		if(!getBtns.length){
			return Ccay.UI.topBox("请输入button的html代码！！！")
		}
		for(var i=0; i < getBtns.length; i++){
			id = 'imgType'+ (i+1);
			tempTable.push("<tr><td style='width:50%'><input type='text' id='{0}'/></td><td>".formatValue(id) + getBtns[i].outerHTML+"</td></tr>");
		};
		$S("#myTable").append(tempTable.join(""));
		$S("input[id*='imgType']").dropdown({
	    	 valid:true
	    }, imgTypes);
	    
		Ccay.UI.init($S("#myTable"));
	}

	// 转化为menubutton
	page.convert = function(){
		
		var getBtn = $S("#btnResourceDiv1").find("input[type='button']");
		if(!getBtn.length){
			return Ccay.UI.topBox("请先输入button的html代码，并选择相应的图标！！！")
		}
		//清空
		$S("#btnTarget1").empty();
		$S("#btnTargetResult1").empty();
		var tempHtml = [];
		tempHtml.push("<div class='init ccay-menubutton'>");
		tempHtml.push("<div class='ccay-menubutton-body'>按钮集合</div>");
		tempHtml.push("<a class='ccay-menubutton-btn' href='javascript:void 0'></a>");
		tempHtml.push("<div class='ccay-menubutton-selector ccay-popup'>");
		tempHtml.push("<ul>");
		var value="";
		$.each(getBtn,function(i){
		    var icon = $S("#imgType"+(i+1)).ccayGetValue();
			
			$(this).removeClass("i18n");
			var i18nKey = $(this).attr("i18nKey");
			$(this).removeAttr("i18nKey");
			if(i18nKey){
				value = "<span class='i18n' i18nKey='"+i18nKey+"'></span>";
			};
			if($(this).attr("value")){
				value = "<span>"+$(this).attr("value")+"</span>"
			}
			$(this).removeAttr("value");
			var getAttr = $(this)[0].outerHTML.replace("<input","").replace("/>",">").replace('value=""',"");
			tempHtml.push("<li " + getAttr + "<a class='ccay-icon {0}'></a>".formatValue(icon) + value);
		})
		tempHtml.push("</ul></div></div>");
		
		$S("#btnTargetResult1").append(tempHtml.join(""));
		$S("#btnTargetResult1").find("li").removeAttr("type").addClass('ccay-menubutton-item');
		$S("#btnTarget1").val($S("#btnTargetResult1")[0].innerHTML);
		
		Ccay.UI.init($S("#btnTargetResult1"));
		
		$S("#btnResourceDiv1").empty();
		$S("#btnResourceDiv1").append($S("#btnResource1").val());
		
	};
	
});

